/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Sep 10, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits.table.actions;

import edu.psu.geovista.app.pcp.toolkits.table.ConfigureTable;

import javax.swing.*;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.event.ActionEvent;
import java.util.Enumeration;
import java.util.Vector;

public class SortAction extends BasicTableAction {

    public SortAction(String name, JComponent owner, ConfigureTable table) {
        super(name, owner, table);

    }

    public void actionPerformed(ActionEvent e) {
        TableColumnModel columnModel = this.table.getColumnModel();
        Enumeration columns = columnModel.getColumns();
        Vector names=new Vector();
        while (columns.hasMoreElements()) {
            TableColumn col = (TableColumn) columns.nextElement();
            Object name = col.getIdentifier();
            names.add(name);
        }

        JComboBox comb=new JComboBox(names);

        Object[] btns=new Object[]{"OK","Cancel"};
                int value=JOptionPane.showOptionDialog(owner, comb,"Please select a column",
                                                        JOptionPane.YES_NO_OPTION,
                                                        JOptionPane.PLAIN_MESSAGE ,
                                                        null,
                                                        btns,
                                                        btns[0]);
                String selectedCol=null;
                if(value==JOptionPane.OK_OPTION ){
                                     //JOptionPane.OK_OPTION map to  "Summary" button
                                     selectedCol = (String) comb.getSelectedItem();
                                    // System.out.println("value:"+selectedCol );


                                 }
                                 else  if(value==JOptionPane.NO_OPTION ||
                                              value==JOptionPane.CLOSED_OPTION ){
                                     //JOptionPane.NO_OPTION map to "Cancel" button
                                     //System.out.println("cancel");
                                     return;

                                 }

                this.table.sort(selectedCol);




    }




}
